<?
function drizzo_global_options($option_name = null) {
	global $drizzo_options;

	drizzo_db_connect();
	if(!$option_name) {
		$options_query = mysql_query("SELECT * FROM `$drizzo_options` WHERE `owner` IS NULL  ORDER BY `ID`");
		if($options_query) {
			while($row = mysql_fetch_array($options_query)) {
				$name = stripslashes($row['name']);
				$drizzo_options_array[$name] = $row['value'];
			}

			return $drizzo_options_array;
		}
	} elseif($option_name) {
		$options_query = mysql_query("SELECT * FROM `$drizzo_options` WHERE `owner` IS NULL AND `name`='$option_name' LIMIT 1");
		if($options_query) {
			while($row = mysql_fetch_array($options_query)) {
				$drizzo_option = $row['value'];
			}

			return $drizzo_option;
		}
	} else {
		return false;
	}
}

function drizzo_global_option($option_name = null) {
	return drizzo_global_options($option_name);
}

function drizzo_user_options($id, $option_name = null) {
	global $drizzo_options;

	drizzo_db_connect();

	if(!$option_name) {
		$options_query = mysql_query("SELECT * FROM `$drizzo_options` WHERE `owner`='$id' ORDER BY `ID`");
		if($options_query) {
			while($row = mysql_fetch_array($options_query)) {
				$name = $row['name'];
				$drizzo_user_options_array[$name] = $row['value'];
			}

			return $drizzo_user_options_array;
		}
	} elseif($option_name) {
		$options_query = mysql_query("SELECT * FROM `$drizzo_options` WHERE `owner`='$id' AND `name`='$option_name' LIMIT 1");
		if($options_query) {
			while($row = mysql_fetch_array($options_query)) {
				$drizzo_user_option = $row['value'];
			}

			return $drizzo_user_option;
		}
	} else {
		return false;
	}
}

function drizzo_user_option($id, $option_name = null) {
	return drizzo_user_options($id, $option_name);
}

function drizzo_update_options() {
	global $drizzo_options;
	if($_POST['submit']) {
		$update_count = 0;
		foreach($_POST as $option => $value) {
			$value = addslashes($value);
			if($option != 'submit') {
				$option_lookup = @mysql_query("SELECT `ID` FROM `$drizzo_options` WHERE `name`='$option'");
				if(mysql_num_rows($option_lookup) > 0) {
					$sql = "UPDATE `$drizzo_options` SET `value`='$value' WHERE `name`='$option'";
				} else {
					$sql = "INSERT INTO `$drizzo_options` (`name`,`value`) VALUES ('$option','$value')";
				}
				$update_options = mysql_query($sql);
				if($update_options) {
					$update_count++;
				}
			}
		}
		if((count($_POST) - 1) == $update_count) {
			return 2;
		} else {
			return 1;
		}
	} else {
		return 0;
	}
}

function drizzo_theme_info($theme = null) {
	if($theme == null) {
		$theme = drizzo_global_options('theme');
	}
	$full_theme_name = $theme;
	$theme = str_replace(' ','',$theme);
	$dir = $_SERVER['DOCUMENT_ROOT'] . "/drizzo-addons/themes/" . $theme;
	$contents = shell_exec("ls " . $dir);
	$contents = explode("\n", $contents);
	$index_search = in_array('index.php', $contents);
	$css_search = in_array('style.css', $contents);
	$screenshot_search = in_array('screenshot.jpg', $contents);
	if($css_search) {
		$theme_meta = file_get_contents($dir . "/style.css", "r");
		$theme_meta = substr($theme_meta, strpos($theme_meta, "/*") + 2, (strpos($theme_meta, "*/") - strpos($theme_meta, "/*") - 2));
		$theme_meta = explode("\n", $theme_meta);
		if($theme_meta) {
			foreach($theme_meta as $meta) {
				$meta = explode(': ', $meta);
				$themeinfo[trim(strtolower($meta[0]))] = trim($meta[1]);
				$themeinfo['nicename'] = str_replace(' ','',$themeinfo['theme']);
			}
		} else {
			$themeinfo['author'] = "unknown";
			$themeinfo['url'] = "unknown";
			$themeinfo['theme'] = ucwords($full_theme_name);
			$themeinfo['version'] = "unknown";
			$themeinfo['nicename'] = "unknown";
		}
	} else {
		$themeinfo['author'] = "unknown";
		$themeinfo['url'] = "unknown";
		$themeinfo['theme'] = ucwords($full_theme_name);
		$themeinfo['version'] = "unknown";
		$themeinfo['nicename'] = "unknown";
	}
	if($index_search && $css_search) {
		$themeinfo['valid'] = "valid";
	} elseif($index_search && !$css_search) {
		$themeinfo['valid'] = "css";
	} elseif(!$index_search && $css_search) {
		$themeinfo['valid'] = "index";
	} else {
		$themeinfo['valid'] = "neither";
	}
	if($screenshot_search) {
		$themeinfo['screenshot'] = "/drizzo-addons/themes/" . $theme . "/screenshot.jpg";
	} else {
		$themeinfo['screenshot'] = admin_folder . "images/no_screenshot.jpg";
	}
	return $themeinfo;
}

function drizzo_list_themes() {
	$theme = str_replace(' ','',drizzo_global_options('theme'));
	$dir = $_SERVER['DOCUMENT_ROOT'] . "/drizzo-addons/themes/";
	$contents = shell_exec("ls " . $dir);
	$contents = explode("\n", $contents);
	if(count($contents) > 0) {
		foreach($contents as $content) {
			if(is_dir($dir . "/" . $content) && ($content != $theme) && ($content != '') && strlen($content) > 0) {
				$themes_list[] = drizzo_theme_info($content);
			}
		}
	}
	if(count($themes_list) > 0) {
		return $themes_list;
	} else {
		return false;
	}
}

function drizzo_list_valid_themes() {
	$themes = drizzo_list_themes();
	if($themes) {
		foreach($themes as $theme) {
			if($theme['valid'] == "valid") {
				$theme_list[] = $theme;
			}
		}
	} else {
		$theme_list = false;
	}
	return $theme_list;
}

function drizzo_list_invalid_themes() {
	$themes = drizzo_list_themes();
	if($themes) {
		foreach($themes as $theme) {
			if($theme['valid'] == "css") {
				$theme_list[] = array(
					"theme" => $theme['theme'],
					"error" => "There is no stylesheet present for this theme."
					);
			}
			if($theme['valid'] == "index") {
				$theme_list[] = array(
					"theme" => $theme['theme'],
					"error" => "There is no index.php present for this theme."
					);
			}
			if($theme['valid'] == "neither") {
				$theme_list[] = array(
					"theme" => $theme['theme'],
					"error" => "There is no index.php or style.css present for this theme."
					);
			}
		}
	} else {
		$theme_list = false;
	}
	return $theme_list;
}

function drizzo_admin_css() {
	$css = drizzo_global_options('admin_css');
	if($css == null || $css == '') {
		$css = "drizzo";
	}
	$css_url = admin_folder . "styles/" . $css . "/" . $css . ".css";
	if(!file_exists($_SERVER['DOCUMENT_ROOT'] . $css_url)) {
		$css_url = "/drizzo-addons/admin-styles/" . $css . "/" . $css . ".css";
	}
	if(!file_exists($_SERVER['DOCUMENT_ROOT'] . $css_url)) {
		$css_url = "/drizzo-addons/admin-styles/" . $css . ".css";
	}
	if(!file_exists($_SERVER['DOCUMENT_ROOT'] . $css_url)) {
		$css_url = admin_folder . "styles/drizzo/drizzo.css";
	}
	return $css_url;
}

function drizzo_admin_styles_dropdown() {
	$current_css = drizzo_global_options('admin_css');
	$base_dir = $_SERVER['DOCUMENT_ROOT'] . admin_folder . "styles/";
	$base_contents = shell_exec("ls " . $base_dir);
	$base_contents = explode("\n", $base_contents);
	if(count($base_contents) > 0) {
		foreach($base_contents as $content) {
			if(strpos($content,'.css')) {
				$styles[] = substr($content, 0, strlen($content) - 4);
			}
			if(is_dir($base_dir . $content) && shell_exec("ls " . $base_dir . $content ." |grep '" . $content . ".css'")) {
				$styles[] = $content;
			}
		}
	}
	$addon_dir = $_SERVER['DOCUMENT_ROOT'] . "/drizzo-addons/admin-styles/";
	$addon_contents = shell_exec("ls " . $addon_dir);
	$addon_contents = explode("\n", $addon_contents);
	if(count($addon_contents) > 0) {
		foreach($addon_contents as $content) {
			if(strpos($content,'.css')) {
				$styles[] = substr($content, 0, strlen($content) - 4);
			}
			if(is_dir($addon_dir . $content) && shell_exec("ls " . $addon_dir . $content ." |grep '" . $content . ".css'")) {
				$styles[] = $content;
			}
		}
	}
	$styles_list = "<select name=\"admin_css\">\n";
	if(count($styles) > 0) {
		foreach($styles as $style) {
			if($style == $current_css) {
				$styles_list .= "<option value=\"$style\" selected>" . ucwords($style) . "</option>\n";
			} else {
				$styles_list .= "<option value=\"$style\">" . ucwords($style) . "</option>\n";
			}
		}
	} else {
		$styles_list .= "<option value=\"drizzo\">Drizzo</option>\n";
	}
	$styles_list .= "</select>\n<br class=\"clear\" />\n";
	return $styles_list;
}
?>